// 绑定地图移动事件
// eslint-disable-next-line no-undef
map.on('moveend', () => {
    // eslint-disable-next-line no-undef
    const {lng, lat} = map.getCenter() //获取当前地图中心位置
    // eslint-disable-next-line no-undef
    map_center = [lng, lat]
    // eslint-disable-next-line no-undef
    functions.change()
})

// 绑定地图缩放事件
// eslint-disable-next-line no-undef
map.on('zoomend', () => {
    // eslint-disable-next-line no-undef
    map_resize = map.getZoom() //获取当前地图级别
    // eslint-disable-next-line no-undef
    functions.change()
})

// 计算两点距离
// eslint-disable-next-line no-unused-vars
function getDistanceBetweenPoints(point1, point2) {
    // eslint-disable-next-line no-undef
    var lngLat1 = new AMap.LngLat(point1[0], point1[1]);
    // eslint-disable-next-line no-undef
    var lngLat2 = new AMap.LngLat(point2[0], point2[1]);
    // 返回两个点之间的距离
    return lngLat1.distance(lngLat2);
}

// 移动到默认中心点
// eslint-disable-next-line no-unused-vars
function locate() {
    // eslint-disable-next-line no-undef
    map.setCenter(map_default) //设置地图中心点
}

// 增加半径
// eslint-disable-next-line no-unused-vars
function addRadius() {
    // eslint-disable-next-line no-undef
    map_search_radiusM = map_search_radiusM + parseInt((map_search_radiusM / 10).toFixed(0))
    fn_radius()
    // eslint-disable-next-line no-undef
    functions.change()
}

// eslint-disable-next-line no-unused-vars
function reduceRadius() {
    // eslint-disable-next-line no-undef
    map_search_radiusM = map_search_radiusM - parseInt((map_search_radiusM / 10).toFixed(0))
    fn_radius()
    // eslint-disable-next-line no-undef
    functions.change()
}

// eslint-disable-next-line no-unused-vars
function fn_radius() {
    // eslint-disable-next-line no-undef
    map.remove(map_circle)
    // eslint-disable-next-line no-undef
    map_circle = new AMap.Circle({
        // eslint-disable-next-line no-undef
        center: map_default, // 圆心坐标
        // eslint-disable-next-line no-undef
        radius: map_search_radiusM, // 半径
        borderWeight: 2, // 边框宽度
        strokeColor: "#47ff33", // 边框颜色
        strokeWeight: 2, // 边框粗细
        strokeOpacity: 1, // 边框透明度
        fillOpacity: 0.2, // 填充颜色透明度
        strokeStyle: 'dashed', // 边框样式
        strokeDasharray: [10, 10], // 虚线样式
        fillColor: '#1791fc', // 填充颜色
        zIndex: 50, // 层级
    }) /*圆*/
    // eslint-disable-next-line no-undef
    map_circle.setMap(map)

    // eslint-disable-next-line no-undef
    circleEditor.close()
    // eslint-disable-next-line no-undef
    circleEditor = new AMap.CircleEditor(map, map_circle)
    // eslint-disable-next-line no-undef
    circleEditor.open()
    // eslint-disable-next-line no-undef
    circleEditor.on('adjust', function(event) {
        // eslint-disable-next-line no-undef
        map_search_radiusM = event.radius
        // eslint-disable-next-line no-undef
        functions.change()
    })
}

// 实例化点标记
// eslint-disable-next-line no-unused-vars
function addMarker(point) {
    // eslint-disable-next-line no-undef
    let url = "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png"
    // eslint-disable-next-line no-undef
    if (point === map_default) {
        url = "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png"
    }
    // eslint-disable-next-line no-undef
    let marker = new AMap.Marker({
        icon: url,
        position: point,
        // eslint-disable-next-line no-undef
        offset: new AMap.Pixel(-26, -60)
    });
    // eslint-disable-next-line no-undef
    marker.setMap(map);
}
// eslint-disable-next-line no-undef
addMarker(map_default)

// 画点
